package com.lipaluma.field.validator.impl.string;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

import com.lipaluma.annotations.validations.Validate;
import com.lipaluma.context.Context;
import com.lipaluma.field.validator.strategies.SubStringMatcherValidationStrategy;
import com.lipaluma.xml.validator.XmlNotEndsWithValidator;
import com.lipaluma.xml.validator.XmlValidator;

/**
 * Validator util for String field that check if this value not ends with given values.<br/>
 *  
 * To initialize :
 * <ul>
 * 	<li>
 * 	<u><b>By annotations :</b></u> Use {@link Validate} and use the <code>notEndsWith</code> attribute to 
 * specify all substrings to check.<br/>
 * 	</li>
 * 	<li>
 * 	<u><b>By Xml :</b></u>
 * <ul>
 * <li>
 * To add the notEndsWith validation, see the example below : <br/>
 * <code>
 * &lt;ToMap field="..." ><br/>
 * <b>
 * &nbsp;&nbsp;&nbsp;&lt;notEndsWith><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value>x&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value>y&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value>z&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&lt;/notEndsWith><br/>
 * </b>
 * &lt;/ToMap><br/>
 * </code>
 * The value of the field must not ends with by 'x', 'y' nor 'z' in this example.
 * </li>
 * </ul>
 * 
 * @author msoares
 *
 */
public class StringNotEndsWithValidator extends AbstractSubStringsForbiddenValidator {

	public StringNotEndsWithValidator() {}
	public StringNotEndsWithValidator(List<String> substrings) {
		super(substrings);
	}
	public StringNotEndsWithValidator(String... substrings) {
		super(substrings);
	}

	public StringNotEndsWithValidator(List<String> substrings, boolean atLeastOne) {
		super(substrings, SubStringMatcherValidationStrategy.ENDS_WITH, atLeastOne);
	}

	public boolean init(Context context, Field field) {
		Validate annotation = field.getAnnotation(Validate.class);
		if(annotation == null || annotation.notEndsWith().length == 0)
			return false;		
		super.init(Arrays.asList(annotation.notEndsWith()), SubStringMatcherValidationStrategy.ENDS_WITH, annotation.atLeastOne());
		return true;
	}

	public boolean init(Context context, Field field, XmlValidator xmlValidator) {
		XmlNotEndsWithValidator validator = (XmlNotEndsWithValidator)xmlValidator;
		super.init(Arrays.asList(validator.getValues()), SubStringMatcherValidationStrategy.ENDS_WITH, validator.isAtLeastOne());
		return true;
	}

}
